package com.david.appletree

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri

object PhoneUtil {

    fun dial(context: Context, phoneNumber: String): Boolean {
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber"))
        if (isIntentAvailable(context, intent)) {
            context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
            return true
        }
        return false
    }

    private fun isIntentAvailable(context: Context, intent: Intent): Boolean {
        return context
            .getPackageManager()
            .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
            .size > 0
    }
}